package com.lxk.util;

import com.lxk.dao.AuctionMapper;
import com.lxk.dao.OrdersMapper;
import com.lxk.pojo.Auction;
import com.lxk.pojo.Orders;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 拍卖定时检查超时订单任务
 *
 * 拍卖回退：wx_user_id与orders_id设为null, ordersStatus设为4(交易关闭),
 * 增加字段：拍卖时间
 *
 * @author JohnSoybean
 * @time 2020年4月1日
 */
public class AuctionOrderTimerTask extends TimerTask {

    private final static long expireMillis = 180000;    //过期时间3分钟
    private final static long expireMillisWithoutOrder = 30000;    //未生成支付订单情况下过期时间30秒

    private Timer timer;
    private Integer auctionId;

    public AuctionOrderTimerTask(Timer timer) {
        this.timer = timer;
    }

    public AuctionOrderTimerTask(Timer timer, Integer auctionId) {
        this.timer = timer;
        this.auctionId = auctionId;
    }

    @Override
    public void run() {
        try {
            AuctionMapper auctionMapper = SpringContextHolder.getBean(AuctionMapper.class);
            OrdersMapper ordersMapper = SpringContextHolder.getBean(OrdersMapper.class);
            Auction auction = auctionMapper.selectByPrimaryKey(auctionId);
            //当执行到小于底价，或已有人支付成功，程序结束
            Date nowTime = new Date();
            if(nowTime.after(auction.getAuctionEndTime())) {
                this.timer.cancel();
                System.out.println("#### 拍卖-超时订单检查进程：结束时间到，任务结束 ####");
                return;
            }
            if(auction.getWxUserId()!=null && auction.getOrdersId()!=null) {
                Orders orders = ordersMapper.selectByPrimaryKey(auction.getOrdersId());
                Integer status = orders.getOrdersStatus();
                //判断是否支付成功
                if(status==1||status==2||status==3) {
                    this.timer.cancel();
                    System.out.println("#### 拍卖-超时订单检查进程：用户支付成功，任务结束 ####");
                }else {
                    //尚未支付成功，执行检查
                    Date buyTime = auction.getBuyTime();
                    Date expireTime = new Date(buyTime.getTime()+expireMillis);
                    if (nowTime.after(expireTime)) {
                        //wx_user_id, orders_id, buy_time设为null, ordersStatus设为4(交易关闭)
                        orders.setOrdersStatus(4);
                        ordersMapper.updateByPrimaryKeySelective(orders);
                        auction.setWxUserId(null);
                        auction.setOrdersId(null);
                        auction.setBuyTime(null);
                        auctionMapper.updateByPrimaryKey(auction);
                        System.out.println("#### 拍卖-超时订单检查进程：支付订单"+orders.getOrdersId()+"超时，已关闭 ####");
                    }
                }
            }else {
                //尚未生成订单，执行检查
                if (auction.getWxUserId()!=null) {
                    Date buyTime = auction.getBuyTime();
                    Date expireTime = new Date(buyTime.getTime()+expireMillisWithoutOrder);
                    if (nowTime.after(expireTime)) {
                        //已有人拍下, wx_user_id, buy_time设为null
                        auction.setWxUserId(null);
                        auction.setBuyTime(null);
                        auctionMapper.updateByPrimaryKey(auction);
                        System.out.println("#### 拍卖-超时订单检查进程：预订单"+auctionId+"超时，已关闭 ####");
                    }
                }
            }
        }catch(Exception e) {
        	e.printStackTrace();
        	System.out.println("#### 拍卖-超时订单检查进程：出现异常 ####");
        	this.timer.cancel();
        }
    }
}
